package com.bibrApp.difu.actvity.account_verify_mvp;


import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bibrApp.difu.R;
import com.bibrApp.difu.actvity.account_info_mvp.AccountInfoActivity;
import com.bibrApp.difu.actvity.df_u_mvp.UActivity;
import com.bibrApp.difu.actvity.login_mvp.LoginActivity;
import com.bibrApp.difu.actvity.pay_setting_mvp.PaySettingActivity;
import com.bibrApp.difu.actvity.security_setting_mvp.SecuritySettingActivity;
import com.bibrApp.difu.actvity.security_strategy_mvp.SecurityActivity;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.utils.IntentUtil;
import com.bibrApp.difu.utils.RegUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.gyf.barlibrary.ImmersionBar;

import java.util.Map;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

public class AccountVerifyActivity extends BaseActivity<AccountVerifyPresenter> implements IAccountVerifyView, TextWatcher {

    private ImmersionBar mImmersionBar;
    private Context mContext;
    private String accountStr;
    private String pwdStr;
    @BindView(R.id.account_tv)
    TextView account_tv;
    @BindView(R.id.register_get_code_tv)
    TextView getCodeTV;
    @BindView(R.id.verify_code_et)
    EditText verify_code_et;
    @BindView(R.id.account_verify_sure_btn)
    Button sureBtn;
    private CountTimer timer;
    @Override
    protected void initPresenter() {
        presenter = new AccountVerifyPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_account_verify;
    }

    @Override
    public void initView() {
        mContext = this;
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColor(R.color.white)
                .statusBarDarkFont(true)
                .fitsSystemWindows(true)
                .init();
        accountStr = getIntent().getStringExtra("account");
        pwdStr = getIntent().getStringExtra("pwd");
        account_tv.setText(RegUtils.regAccount(accountStr));

        verify_code_et.addTextChangedListener(this);
    }


    @Override
    public void error(String msg) {
        ToastUtils.ShowToast(mContext, msg);
    }

    @Override
    public void success(String msg) {
        if(timer==null) {
            timer = new CountTimer(60000, 1000);
        }
        timer.start();
        getCodeTV.setEnabled(false);
        getCodeTV.setClickable(false);
        ToastUtils.ShowToast(mContext, msg);
    }

    @Override
    public void successOfRegister(String msg) {
        IntentUtil.toActivity(LoginActivity.class,this);
    }

    @OnClick(R.id.register_get_code_tv)
    public void onClickGetCode(){
        getCode();
    }

    @OnClick(R.id.account_verify_sure_btn)
    public void onClickRegister(){
        presenter.register(accountStr,pwdStr,verify_code_et.getText().toString().replaceAll(" ", ""),"");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }
        if (timer != null) {
            timer.cancel();
        }

    }

    /**
     * 获取验证码
     */
    private void getCode() {
        presenter.getCode(accountStr);
    }

    class CountTimer extends CountDownTimer {

        public CountTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            getCodeTV.setEnabled(true);
            getCodeTV.setClickable(true);
            getCodeTV.setText("点击获取");
            getCodeTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getCode();
                }
            });
        }

        @Override
        public void onTick(long millisUntilFinished) {
            getCodeTV.setText((millisUntilFinished / 1000) + "S" + "重新获取");
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String firstETStr = verify_code_et.getText().toString().replaceAll(" ", "");
        if (!TextUtils.isEmpty(firstETStr) && firstETStr.length() >= 4) {
            sureBtn.setEnabled(true);
            sureBtn.setClickable(true);
        } else {
            sureBtn.setEnabled(false);
            sureBtn.setClickable(false);
        }
    }

}
